/*
 * Created: 2022-08-25 20:40:37
 * Author : 涛涛涛
 */

import { getDomInfo } from './getDomInfo';
import Taro from '@tarojs/taro';
import { useEffect, useState } from 'react';

/** 计算元素的高度,方便为之搞一个占位符. */
export const useDomHeight = (domName: string) => {
    const [domHeight, setDomHeight] = useState(0);
    useEffect(() => {
        const configHeight = async () => {
            setTimeout(async () => {
                const { height } = await getDomInfo(domName);
                setDomHeight(height);
                //增加300ms延迟,因为,tabbar的渲染带图标的要再慢上一步,所以要等图标出现后,再获得高度
            }, 300);
        };
        configHeight();
        window.addEventListener('resize', configHeight);
        return () => {
            window.removeEventListener('resize', configHeight);
        };
    }, [domName]);
    return { domHeight };
};
